<template>
  <el-dialog :title="formData.id?'编辑房产':'添加房产'" :visible="showDialog" @close="close">
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="经纪">
        <el-select v-model="formData.agent">

          <el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="formData.category">
          <el-option v-for="item in categoryList" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="标签">
        <el-tag
          v-for="tag in formData.tags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="formData.address" />
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="formData.price" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quillEditor v-model="formData.content" />
      </el-form-item>
      <el-form-item label="介绍图片">
        <UploadImages ref="photo" :img-list="imgList=false" @getImg="getPhoto" @delPhotos="delPhotos" />
        <el-input v-model="formData.photos" placeholder="强制设置图片跳过上传" />
      </el-form-item>
      <el-form-item label="封面">
        <UploadImages ref="image" @getImg="getImg" @delImg="delImg" />
        <el-input v-model="formData.image" placeholder="强制设置图片跳过上传" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { addProperties, editPropperties, getPropertiesById } from '../../../api/properties'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  components: {
    quillEditor
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    agentList: {
      type: Array,
      default() {
        return []
      }
    },
    categoryList: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      formData: {
        agent: '',
        category: '',
        title: '',
        tags: [],
        address: '',
        price: '',
        desc: '',
        content: '',
        photos: [],
        image: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ]
      },
      inputVisible: false,
      inputValue: '',
      imgList: false,
      imgUrl: ''
    }
  },
  methods: {
    // 数据回显
    async getInfo(id) {
      const res = await getPropertiesById(id)
      this.formData = res.data

      this.$refs.image.test = this.formData.image
      // console.log('130', this.$refs.image)

      // this.$refs.photo.urlList = this.formData.photos
      this.$refs.photo.urlList = this.formData.photos.map(item => ({ url: item }))
    },
    getPhoto(img) {
      this.formData.photos.push(img)
    },
    getImg(img) {
      // console.log(img)
      this.formData.image = img
    },
    // 点击删除图片时

    delPhotos(fileList) {
      const index = this.formData.photos.findIndex(item => {
        return item === fileList.url
      })
      console.log('图片下标', index)
      this.formData.photos.splice(index, 1)
    },
    // 删除封面
    delImg(img) {
      this.formData.image = img
    },
    // 添加房产
    async submit() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await editPropperties(this.formData)
      } else {
        await addProperties(this.formData)
      }
      this.$message.success('操作成功')
      this.close()
      this.$emit('updateFn')
    },
    close() {
      this.$emit('update:showDialog', false)
      this.formData = {
        agent: '',
        category: '',
        title: '',
        tags: [],
        address: '',
        price: '',
        desc: '',
        content: '',
        photos: [],
        image: ''
      }
      // this.tagsList = ''
      this.$refs.form.resetFields()
      this.$refs.image.removeImg()
      this.$refs.photo.removeImg()
    },
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        // this.tagsList.push(inputValue)
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>
<style lang="scss" scoped>
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

</style>
